﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liger.Web.Common;
using Liger.Web.Common.Extensions;
using System.ComponentModel;

namespace Liger.Web.UI
{ 
    public class GridAjaxSettingsBuilder
    {
        private readonly GridPagingSettings paging;
        private readonly GridAjaxSettings ajax;
        private readonly GridBaseSettings settings;
        public GridAjaxSettingsBuilder(GridPagingSettings paging,GridAjaxSettings ajax,GridBaseSettings settings)
        {
            this.paging = paging;
            this.ajax = ajax;
            this.settings = settings;
        }

        /// <summary>
        /// 是否启用Ajax(使用服务器数据)
        /// </summary>
        /// <param name="inLocal"></param>
        /// <returns></returns>
        public GridAjaxSettingsBuilder Enabled(bool enabled)
        {
            if (enabled)
            { 
                this.settings.Data = null;
            }
            else
            { 
                this.ajax.URL = ""; 
            }
            return this;
        }

         
        /// <summary>
        /// set ajax url
        /// </summary>
        public GridAjaxSettingsBuilder URL(string url)
        {
            this.ajax.URL = url;
            return this;
        }

        /// <summary>
        /// 设置 提交到服务器的参数,可动态，
        /// 比如AjaxParm("OrderID",() => "CurrentOrderData.OrderID") 解析出来将是：
        /// { "OrderID" : CurrentOrderData.OrderID }
        /// </summary>
        public GridAjaxSettingsBuilder AjaxParm(string name, Func<string> function)
        {
            this.ajax.Parms[name] = function;
            return this;
        }

        /// <summary>
        /// 设置 提交到服务器的参数
        /// </summary>
        public GridAjaxSettingsBuilder AjaxParm(string name, string value)
        {
            this.ajax.Parms[name] = () => "'" + value + "'";
            return this;
        }

        /// <summary>
        /// 设置 提交到服务器的参数
        /// </summary>
        public GridAjaxSettingsBuilder AjaxParm(string name, int value)
        {
            this.ajax.Parms[name] = () => "'" + value + "'";
            return this;
        }

        /// <summary>
        /// 设置 状态名
        /// </summary>
        public GridAjaxSettingsBuilder StatusName(string value)
        {
            this.ajax.StatusName = value;
            return this;
        }

        /// <summary>
        /// 设置 提交方式
        /// </summary>
        public GridAjaxSettingsBuilder Method(string value)
        {
            this.ajax.Method = value;
            return this;
        }

        /// <summary>
        /// 设置 数据源字段名
        /// </summary>
        public GridAjaxSettingsBuilder Root(string value)
        {
            this.ajax.Root = value;
            return this;
        } 

        /// <summary>
        /// 设置 数据源记录数字段名
        /// </summary>
        public GridAjaxSettingsBuilder Record(string value)
        {
            this.ajax.Record = value;
            return this;
        } 

        /// <summary>
        /// 设置 页索引参数名，(提交给服务器)
        /// </summary>
        public GridAjaxSettingsBuilder PageParmName(string value)
        {
            this.ajax.PageParmName = value;
            return this;
        } 

        /// <summary>
        /// 设置 页记录数参数名，(提交给服务器)
        /// </summary>
        public GridAjaxSettingsBuilder PagesizeParmName(string value)
        {
            this.ajax.PagesizeParmName = value;
            return this;
        } 
        /// <summary>
        /// 设置 页排序列名(提交给服务器)
        /// </summary>
        public GridAjaxSettingsBuilder SortnameParmName(string value)
        {
            this.ajax.SortnameParmName = value;
            return this;
        } 
        /// <summary>
        /// 设置 页排序方向(提交给服务器)
        /// </summary>
        public GridAjaxSettingsBuilder SortorderParmName(string value)
        {
            this.ajax.SortorderParmName = value;
            return this;
        }

        /// <summary>
        /// 页排序列名
        /// </summary>
        public GridAjaxSettingsBuilder SortName(string value)
        {
            this.ajax.SortName = value;
            return this;
        }
        /// <summary>
        /// 页排序方向
        /// </summary>
        public GridAjaxSettingsBuilder SortOrder(string value)
        {
            this.ajax.SortOrder = value;
            return this;
        }
    }
}
